function News() {
    this.progressGroup = $("#progress-group");
};

News.prototype.initUEditor = function(){
    window.ue = UE.getEditor('editor',{
        'initialFrameHeight':400,
        'serverUrl':'/ueditor/upload/'
    });
};


News.prototype.listenUploadFielEvent = function(){
   var uploadBtn = $("#thumbnail-btn");
   uploadBtn.change(function () {
       // 只有一个#thumbnail-btn 所以取第零个
       var file = uploadBtn[0].files[0];
       var formData = new FormData();
       formData.append("file", file);
       xfzajax.post({
           "url":"/cms/upload_file/",
           "data":formData,
           "processData":false,
           "contentType":false,
           "success":function (result) {
               if(result["code"] === 200){
                   // 设置url到input中
                   var url = result["data"]["url"];
                   var thumbnailInput = $("#thumbnail-form");
                   thumbnailInput.val(url);
               }
           }
       })
   })
};

News.prototype.listenQiniuUploadFileEvent = function(){
    var self = this;
    var uploadBtn = $("#thumbnail-btn");
    var progressBar = $(".progress-bar");
    uploadBtn.change(function () {
        progressBar.css({"width":0});
        var file = this.files[0];
        xfzajax.get({
            'url':'/cms/qntoken/',
            'success':function (result) {
                if(result['code'] === 200){}
                var token = result['data']['token'];
                // a.b.jpg = ['a','b','jpg']
                // 198888888 + . + jpg = 1988888.jpg
                var key = (new Date()).getTime() + '.' + file.name.split('.')[1]
                var putExtra = {
                    fname : key,
                    params : {},
                    mimeType : ['image/png','image/jpeg','image/gif','video/x-ms-wmv', 'video/mp4']

                };
                var config = {
                    useCdnDomain : true,
                    retryCount :6,
                    region : qiniu.region.z2

                };
                var observable = qiniu.upload(file, key, token, putExtra, config);
                observable.subscribe({
                    'next': self.handleFileUploadProgress,
                    'error': self.handleFileUploadError,
                    'complete': self.handleFileUploadComplete
                });

            }
        })
    })
};

News.prototype.handleFileUploadProgress = function(response){
    var total = response.total;
    var percent = total.percent;
    var percentText = percent.toFixed(0)+'%';
    // 百分条进度展示
    var progressGroup = News.progressGroup;
    progressGroup.show();
    var progressBar = $(".progress-bar");
    progressBar.css({"width":percentText});
    progressBar.text(percentText);


};

News.prototype.handleFileUploadError = function(error){

    window.messageBox.showError(error.message);
    var progressGroup = $("#progress-group");
    progressGroup.hide();
    console.log(error.message);

};

News.prototype.handleFileUploadComplete = function(response){

    console.log(response);
    var progressGroup = $("#progress-group");
    progressGroup.hide();

    var domain = "http://q0pbckv8o.bkt.clouddn.com/";
    var filename = response.key;
    var url = domain + filename;
    var thumbnailInput = $("input[name='thumbnail']");
    thumbnailInput.val(url);



};

News.prototype.listenSubmitEvent = function(){
  var submitBtn = $("#submit-btn");
  submitBtn.click(function (event) {
      event.preventDefault();
      var btn = $(this);
      var pk = btn.attr('data-news-id');

      var title = $("input[name='title']").val();
      var category = $("select[name='category']").val();
      var desc = $("input[name='desc']").val();
      var thumbnail = $("input[name='thumbnail']").val();
      var content = window.ue.getContent();

      // var url = '';
       if(pk){
              url = '/cms/edit_news/';
          }else{
             url = '/cms/write_news/';
           }
       xfzajax.post({
           'url':url,
           'data':{
               'title':title,
               'category': category,
               'desc': desc,
               'thumbnail': thumbnail,
               'content': content,
               'pk': pk
           },
           'success':function (result) {
               if(result['code'] === 200){
                   xfzalert.alertSuccess('恭喜！新闻发表成功', function () {
                       window.location.reload();
                   })
               }
           }
       })

  })
};



News.prototype.run = function () {
    var self = this;
    self.initUEditor();
    self.listenUploadFielEvent();
    self.listenQiniuUploadFileEvent();
    self.listenSubmitEvent();
};

$(function () {
    var news = new News();
    news.run();

    // 因为实在方法内执行的，所以必须单独定义调用
    News.progressGroup = $("#progress-group");
});